<?php
// 4/10/20010: Immanuel192 rewrite this module

class slidemodule {
	function name() { return "Songs Slideshow Module"; }
	function description() { return "Songs slideshow for church services"; }
	function author() { return "ABSoft.vn"; }

	function hasSources() { return true; }
	function hasContent() { return true; }
	function hasViews() { return true; }

	function supportsWorkflow() { return false; }

	// type = 0: return normal permissions array
	// type = 1: return permissions array for $template->register_permission
	function permissions($internal = "", $type=0) {
		if ($type == 0) {
			return array(
				'administrate'=>'Administrate',
				'configure'=>'Configure',
				'create'=>'Add songs',
				'edit'=>'Edit songs',
				'delete'=>'Delete songs'
			);
		}
		else
		{
			return array('administrate','configure','create','edit','delete');
		}
	}

	function show($view,$loc = null, $title = "") {
		global $db;
		
		$config = $db->selectObject('slidemodule_config',"location_data='".serialize($loc)."'");
		if ($config == null) {
			
		}
		
		// GET ALL SONG COLLECTIONS
		$song_cols=$db->selectObjects("present_song_collection");
		// END OF SONG COLLECTIONS GET
		// GET ALL SONGS IN CURRENT PLAYLIST
		// remove old playlist
		$time=time();
		// get all playlist we want to delete
		$pl_delete=$db->selectObjects("present_playlist","create_time  < " . ($time-1800));
		foreach ($pl_delete as $pl_k=>$pl_v)
			$db->delete("present_playlist_songs","playlist_id = {$pl_v->id}");
		$db->delete("present_playlist","create_time  < " . ($time-1800));
		
		// get current playlist
		$playlist=exponent_sessions_getTicketString();
		// insert new playlist if it is not availible
		$pl=$db->selectObject("present_playlist","session = '{$playlist}'");
		if (!$pl)
		{
			$pl=null; $pl->session=$playlist; $pl->create_time=$time;
			$pl->id = $db->insertObject($pl,"present_playlist");
		}
		else
		{
			//update create_time
			$pl->create_time=$time;
			$db->updateObject($pl,"present_playlist");
		}
		$sql="SELECT {$db->prefix}present_songs.*, {$db->prefix}present_playlist_songs.rank rank FROM {$db->prefix}present_songs INNER JOIN {$db->prefix}present_playlist_songs
		      ON {$db->prefix}present_songs.id = {$db->prefix}present_playlist_songs.song_id
		      WHERE {$db->prefix}present_playlist_songs.playlist_id = " . $pl->id . "
		      ORDER BY rank";
		$pl_songs=$db->selectObjectsBySql($sql);
		
		// END OF GET ALL SONGS IN CURRENT PLAYLIST
		
		// GET ALL SONGS IN EACH COLLECTION
		foreach ($song_cols as $key=>$value)
		{
			$songs = $db->selectObjects("present_songs","collection_id = " . $value->id . " ORDER BY name");
			// remove songs in current playlist
			foreach ($pl_songs as $k=>$v)
			{
				foreach ($songs as $k1=>$v1)
					if ($v->id == $v1->id)
						unset($songs[$k1]);
			}
			$value->songs=null; $value->songs = $songs;
			$song_cols[$key]=$value;
		}
		// END OF GET ALL SONG IN EACH COLLECTION
		
		exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
		$template = new template('slidemodule',$view,$loc);
		$template->assign('moduletitle',$title);
		$template->assign('song_cols',$song_cols);
		$template->assign('playlist',$pl);
		$template->assign('current_playlist_songs',$pl_songs);
		$template->register_permissions(slidemodule::permissions("",1),$loc);
		$template->output();
	}

	function deleteIn($loc) {
		
		
	}

	function copyContent($oloc,$nloc) {
	}
	

	function spiderContent($item = null) {
		return false;
	}
}

?>
